package com.example.springbootdemo.controller;

import com.example.springbootdemo.dao.UserRepository;
import com.example.springbootdemo.dto.User;
import com.example.springbootdemo.entity.Tuser;
import com.example.springbootdemo.mapper.UserMapper;
import com.example.springbootdemo.service.UserServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@Api(tags = "用户管理")
@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    UserServiceI userService;
    @Autowired
    UserMapper userMapper;
    @ApiOperation("根据用户编号/id获取用户信息")
    @GetMapping("/{id}")
    public User getUserById(@ApiParam("用户编号") @PathVariable("id") int id){
        return userService.getUserById(id);
    }
    @ApiOperation("获取所有用户信息")
    @GetMapping("")
    public List<User> getAll(){
        return userService.allUsers();
    }
    @PostMapping("")
    @ApiOperation("添加新用户")
    public User addUser(@ApiParam("新增用户的信息，只需要填写name") @RequestBody User user){
        return userService.addUser(user);
    }

    @ApiOperation("根据用户编号/id获取用户信息(MyBatis)")
    @GetMapping("/my/{id}")
    public User getUserByIdMyBatis(@ApiParam("用户编号") @PathVariable("id") int id){
        return userMapper.getById(id);
    }
    @ApiOperation("获取所有用户信息(MyBatis)")
    @GetMapping("/my")
    public List<User> getAllMyBatis(){
        return userMapper.getAllUsers();
    }
}
